package com.own.component.util.excel.util.read.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.own.compoent.util.zip.unzip.model.BaseZipFileModel;
import com.own.component.util.excel.exception.ExcelException;
import com.own.component.util.excel.method.WorkbookMethod;
import com.own.constant.ConstantCommon;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;

/**
 * ResolveFileModel
 *
 * @author chenxueli
 * @date 2022-10-07 20:36:14
 */
@Slf4j
@Getter
public class ResolveFileModel implements Serializable {

    /**
     * 上传的文件名称
     */
    private String fileName;
    /**
     * 文件后缀
     */
    private String suffix;
    /**
     * 文件长度
     */
    private long fileSize;
    /**
     * 文件流信息
     */
    @JsonIgnore
    private transient InputStream inputStream;

    /**
     * spring 文件上传的接收对象
     *
     * @param multipartFile 文件信息
     */
    public ResolveFileModel(MultipartFile multipartFile) {
        if (multipartFile == null) {
            return;
        }
        try {
            this.inputStream = multipartFile.getInputStream();
        } catch (IOException e) {
            throw new ExcelException("解析文件错误");
        }
        this.fileName = multipartFile.getOriginalFilename();
        this.fileSize = multipartFile.getSize();
        this.suffix = gainSuffix(this.fileName);
    }

    /**
     * spring 文件上传的接收对象
     *
     * @param inputStream 文件流信息
     */
    public ResolveFileModel(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    /**
     * spring 文件上传的接收对象
     *
     * @param fileModel 压缩文件信息
     */
    public ResolveFileModel(BaseZipFileModel fileModel) {
        this.inputStream = fileModel.getContent();
        this.fileName = fileModel.getFilename();
        this.suffix = gainSuffix(this.fileName);
    }

    /**
     * 文件上传
     *
     * @param path 文件路径
     */
    public ResolveFileModel(String path) {
        this(new File(path));
    }

    /**
     * 文件上传
     *
     * @param file 文件信息
     */
    public ResolveFileModel(File file) {
        if (file == null) {
            return;
        }
        try {
            this.inputStream = new FileInputStream(file);
        } catch (IOException e) {
            throw new ExcelException("解析文件错误");
        }
        this.fileName = file.getName();
        this.fileSize = file.length();
        this.suffix = gainSuffix(this.fileName);
    }

    /**
     * 清空文件流信息
     */
    public void close() {
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            inputStream = null;
        }
    }

    /**
     * 根据文件名称获取文件后缀
     *
     * @param fileName 文件名
     * @return 文件后缀
     */
    private String gainSuffix(String fileName) {
        if (fileName == null) {
            return "";
        }
        if (!fileName.contains(ConstantCommon.POINT)) {
            return "";
        }
        var suffix = fileName.substring(fileName.lastIndexOf(ConstantCommon.POINT)).toLowerCase();
        if (suffix.length() == 0) {
            return "";
        }
        return suffix;
    }

    /**
     * 获取新的文件流信息
     *
     * @return 复制的对象
     */
    public InputStream newInputStream() {
        var array = WorkbookMethod.copyInputStream(inputStream);
        // 赋值给原来的对象
        this.inputStream = array[0];
        // 返回新的对象
        return array[1];
    }

    @Override
    public String toString() {
        return "{" +
                "fileName='" + fileName + '\'' +
                ", suffix='" + suffix + '\'' +
                ", fileSize=" + fileSize +
                '}';
    }

}
